package com.android.view.widget.pop;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.android.R;
import com.android.util.WindowUtils;
import com.android.view.adapter.PopSelectListAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by TenFace
 * on 2017/9/4.
 */

public class SelectListPop {

    private SelectListPop() {
    }

    public static SelectListPop getInstance() {
        return SelectListHolder.instance;
    }

    private static class SelectListHolder {
        static SelectListPop instance = new SelectListPop();
    }

    private PopupWindow popupWindow;
    private View rootView;
    private Unbinder bind;
    private PopSelectListAdapter adapterTop;
    private PopSelectListAdapter adapterBottom;
    @BindView(R.id.pop_top_list_recycler_view)
    RecyclerView recyclerViewTop;
    @BindView(R.id.pop_bottom_list_recycler_view)
    RecyclerView recyclerViewBottom;


    private void createView(View view, int topPosition, int bottomPosition, List<String> dataTop, List<String> dataBottom, final PopSelectListAdapter.ItemClick click) {
        Context context = view.getContext();
        rootView = LayoutInflater.from(context).inflate(R.layout.pop_market_list, null);
        bind = ButterKnife.bind(this, rootView);
        Rect rect = new Rect();
        view.getGlobalVisibleRect(rect);
        int width = rect.width();
        popupWindow = new PopupWindow(rootView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                bind.unbind();
                popupWindow = null;
                rootView = null;
                adapterTop = null;
                adapterBottom = null;
                click.close();
            }
        });
        initData(context, topPosition, bottomPosition, dataTop, dataBottom, click);
    }

    private void initData(Context context, int topPosition, int bottomPosition, List<String> dataTop, List<String> dataBottom, PopSelectListAdapter.ItemClick click) {
        adapterTop = new PopSelectListAdapter();
        LinearLayoutManager msTop = new LinearLayoutManager(context);
        msTop.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewTop.setLayoutManager(msTop);
        recyclerViewTop.setAdapter(adapterTop);
        adapterTop.setIndex(topPosition);
        adapterTop.refreshData(dataTop);
        adapterTop.setItemClickListener(click, 0);

        adapterBottom = new PopSelectListAdapter();
        LinearLayoutManager msBottom = new LinearLayoutManager(context);
        msBottom.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewBottom.setLayoutManager(msBottom);
        recyclerViewBottom.setAdapter(adapterBottom);
        adapterBottom.setIndex(bottomPosition);
        adapterBottom.refreshData(dataBottom);
        adapterBottom.setItemClickListener(click, 1);


    }

    public void show(View view, int topPosition, int bottomPosition, List<String> dataTop, List<String> dataBottom, PopSelectListAdapter.ItemClick click) {
        close();
        createView(view, topPosition, bottomPosition, dataTop, dataBottom, click);
        if (popupWindow != null && !popupWindow.isShowing()) {
            Rect rect = new Rect();
            view.getGlobalVisibleRect(rect);
            int left = rect.left;
            int top = rect.top + WindowUtils.dip2px(view.getContext(), 2);
            popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, left, top);
        }
    }

    public void close() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }


}
